﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using OpenPhotoAlbum.BusinessLogics;
using OpenPhotoAlbum.Controls;

namespace OpenPhotoAlbum
{
	public partial class Default : AlbumPage
	{
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			// set title
			this.Title = StringUtility.AddAppPrefix(Resources.Strings.Title_WebSiteHome);

			// set strings and basic layout
			bool isUserLoggedIn = BusinessLogics.UserManager.Instance.IsUserLoggedIn;
			pnlQuickLoginAnonymous.Visible = !isUserLoggedIn;
			pnlQuickLoginRegistered.Visible = isUserLoggedIn;
			hlJoin1.Text = Resources.Strings.CreateYourAccount;
			hlJoin1.NavigateUrl = Links.Join;
			hlJoin1.Visible = !isUserLoggedIn;
			hlEdit.NavigateUrl = Links.MyHome;
			hlEdit.Text = Resources.Strings.SC_Edit;
			hlPublic.NavigateUrl = UrlContext.BuildSharedHomeUrl(HttpContext.Current.User.Identity.Name);
			hlPublic.Text = Resources.Strings.SC_Public;
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			// Ask CacheManager to check whether it is the
			// time to recycle the cache or not.
			if (!IsPostBack) { CacheManager.Instance.RecycleIfNecessary(); }
		}

		protected override void AppendSiteMapNodes(OpenPhotoAlbum.Controls.AlbumSiteMapProvider provider)
		{
			base.AppendSiteMapNodes(provider);

			// Customize the breadcrumb.
			// For example: Home
			provider.AppendSiteMapNode(Resources.Strings.SC_Home, RawUrl);
		}

		// The salt used for generating user-specific cache keys.
		private static readonly Guid SaltMyPhotos = Guid.NewGuid();
		private static readonly Guid SaltMyComments = Guid.NewGuid();

		/// <summary>
		/// The summary information of current identified user.
		/// For example: "You have uploaded 1 photos, and received 0 comments."
		/// </summary>
		protected string MySummary
		{
			get
			{
				// Generate user-specific cache keys.
				Guid cacheKeyMyPhotos = GuidUtility.Combine(SaltMyPhotos, this.UserId);
				Guid cacheKeyMyComments = GuidUtility.Combine(SaltMyComments, this.UserId);

				int photos, comments;

				if (CacheManager.Instance.ContainsKey(cacheKeyMyPhotos)
					&& CacheManager.Instance.ContainsKey(cacheKeyMyComments))
				{
					// Read data from cache.
					photos = CacheManager.Instance.ReadCache<int>(cacheKeyMyPhotos);
					comments = CacheManager.Instance.ReadCache<int>(cacheKeyMyComments);
				}
				else
				{
					// Read data from database.
					photos = BusinessLogics.UserManager.Instance.GetUserPhotoCount(this.UserId);
					comments = BusinessLogics.UserManager.Instance.GetUserCommentCount(this.UserId);
					
					// Write data to cache.
					CacheManager.Instance.WriteCache<int>(cacheKeyMyPhotos, photos);
					CacheManager.Instance.WriteCache<int>(cacheKeyMyComments, comments);
				}

				// Return the formatted string result.
				return StringUtility.Format(Resources.Strings.InfoBar_MySummary, photos, comments);
			}
		}
	}
}
